package com.sunstar.club.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sunstar.club.subject.application.convert.SubjectCategoryDtoConverter;
import com.sunstar.club.subject.application.dto.SubjectCategoryDto;
import com.sunstar.club.subject.common.result.Result;
import com.sunstar.club.subject.domain.entity.SubjectCategoryBo;
import com.sunstar.club.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目分类controller
 *
 * @Author: Shylo Sun
 * @Project: track-club
 * @Date: 2024/2/15 23:17
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 新增分类
     *
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }

            // 参数校验
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(), "分类类型不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryName(), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(), "分类父级id不能为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.convertBoToCategory(subjectCategoryDto);
            subjectCategoryDomainService.add(subjectCategoryBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增分类失败");
        }
    }

    /**
     * 查询分类
     *
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDto>> queryPrimaryCategory(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.convertDtoToCategoryBo(subjectCategoryDto);
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBo);
            List<SubjectCategoryDto> subjectCategoryDtoList = SubjectCategoryDtoConverter.INSTANCE.convertBoToCategoryDtoList(subjectCategoryBoList);
            return Result.ok(subjectCategoryDtoList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询分类失败");
        }
    }

    /**
     * 查询大类下分类
     *
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(), "分类父级id不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.convertDtoToCategoryBo(subjectCategoryDto);
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBo);
            List<SubjectCategoryDto> subjectCategoryDtoList = SubjectCategoryDtoConverter.INSTANCE.convertBoToCategoryDtoList(subjectCategoryBoList);
            return Result.ok(subjectCategoryDtoList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询分类失败");
        }
    }

    /**
     * 更新分类
     *
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.convertDtoToCategoryBo(subjectCategoryDto);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBo);
            return Result.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("更新分类失败");
        }
    }

    /**
     * 删除分类
     *
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.convertDtoToCategoryBo(subjectCategoryDto);
            Boolean result = subjectCategoryDomainService.delete(subjectCategoryBo);
            return Result.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("删除分类失败");
        }
    }
}
